﻿using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Text;

namespace DifyToOpenAI.Common
{
    public static class LogHelperServices
    {
        public static void CommonApiResponseLog(ILogger _logger, ControllerBase controllerBase, ContentResult resContent)
        {
            try
            {
                string ipAddress = controllerBase.HttpContext.Connection.RemoteIpAddress!.ToString();
                string requestLine = controllerBase.Request.Method + "  " + controllerBase.Request.GetDisplayUrl() + "  " + controllerBase.Request.Protocol;
                StringBuilder stringBuilderlog = new StringBuilder();
                stringBuilderlog.AppendLine("");
                stringBuilderlog.AppendLine("Form IpAddress: " + ipAddress);
                stringBuilderlog.AppendLine("Request Url: " + requestLine);
                stringBuilderlog.AppendLine("Response StatusCode: " + resContent.StatusCode);
                stringBuilderlog.AppendLine("Response Body: ");
                stringBuilderlog.AppendLine(resContent.Content);
                _logger.LogInformation(stringBuilderlog.ToString());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                // throw;
            }
        }
    }
}
